<?php
/* @var $this DetailController */

$stand_breadcrums = array(
   $category['cat_name'] => '/category/' . $category['cat_id'] . '/' . CVietnameseTools::makeUrlFriendly($category['cat_name']) . '/',
   $book_info['book_name']
);

$this->breadcrumbs= $stand_breadcrums;
?>
<div class="detail-box">
   <div class="top-content relative">
      <div class="cover left">
         <img src="/uploads/pictures/product/<?php echo $book_info['book_image']?>" />
      </div>
      <div class="info">
         <h1 class="font-14"><?php echo $book_info['book_name']?></h3>
         <p class="price">Giá: <?php echo Str::formatNumber($book_info['book_price'])?> đ</p>
         <p class="num-page">Số trang: <?php echo Str::formatNumber($book_info['book_num_page'])?></p>
         <p class="publisher">Nhà xuất bản: <?php echo $publisher['pub_name']?></p>
         <p class="category">Chuyên mục: <?php echo $category['cat_name']?></p>
         <?php if(count($authors) > 0){
            $author = '';
            ?>
            <p class="author">Tác giả:
            <?php foreach($authors as $item){
               $author .= $item['aut_name'] . ', ';
            }
               $author = substr($author,0,-2);
               echo $author;
            ?>
            </p>
         <?php }?>
         
         <div class="option absolute">
            <p class="btn btn-info add-to-cart" onclick="adToCart(<?php echo $book_info['book_id']?>)">Giỏ hàng</p>
         </div>
                    
      </div>
   </div><!-- End top-content -->
   <div class="bottom-content">
      <!-- Nav tabs -->
      <ul class="nav nav-tabs">
        <li class="active"><a href="#description" data-toggle="tab">Mô tả</a></li>
        <li><a href="#review" data-toggle="tab">Đánh giá</a></li>
      </ul>
      <!-- Tab panes -->
      <div class="tab-content">
        <div class="tab-pane active" id="description">
         <?php echo $book_info['book_content']?>
        </div>
        <div class="tab-pane" id="review">
         <div class="list-review">
            <?php if(count($reviews) == 0){
               ?>
               <p>Chưa có đánh giá nào</p>
            <?php }else{
               $i = 0;
               $total_rate = 0;
               foreach($reviews as $item){
                  $i++;
                  //$use_info = UserIdentity::getInfo($item['rev_use_id'],array('use_fullname'));
                  //$use_fullname = 'Chinh';//$use_info['use_fullname'];
                  $user_rate = Member::model()->findByPk($item['rev_use_id']);
                  $use_fullname = $user_rate->MemberName;
                  $total_rate += $item['rev_rating'];
                  $width = $item['rev_rating'] * 20;
                  $date = date('H:i:s - d/m/Y',$item['rev_date']);?>
                  <div class="review-item relative">
                     <div class="absolute rating"><span title="<?php echo $item['rev_rating']?> sao" class="current_rating" style="width: <?php echo $width?>%;"></span></div>
                     <p class="review-user"><strong><?php echo $use_fullname?></strong></p>
                     <i class="review-time font-11"><?php echo $date?></i>
                     <div class="review-content"><?php echo $item['rev_content']?></div>
                  </div>
               <?php }
               ?>
               
            <?php 
            $width_avg = $total_rate/$i * 20;?>
               <p>Trung bình</p>
               <div class="rating">
                  <span title="<?php echo round($total_rate/$i,2) ?> sao" class="current_rating" style="width: <?php echo $width_avg?>%;"></span>
               </div>
            <?php  }?>
            
         </div>
         <p class="btn btn-default" data-toggle="modal" data-target=".modal">Thêm đánh giá</p>
        </div>
        <!-- Small modal -->
         
         <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
           <div class="modal-dialog">
             <div class="modal-content">
               <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h4 class="modal-title">Đánh giá của bạn</h4>
               </div>
               <div class="modal-body">
                  <form method="POST" action="">
                     <textarea name="rev_content" id="rev_content"></textarea>
                     <div class="rating relative">
                        <span style="width: 0%;" class="current_rating" id="current_rating"></span>
                        <span class="rate rate-1 absolute" rate="1"></span>
                        <span class="rate rate-2 absolute" rate="2"></span>
                        <span class="rate rate-3 absolute" rate="3"></span>
                        <span class="rate rate-4 absolute" rate="4"></span>
                        <span class="rate rate-5 absolute" rate="5"></span>
                     </div>
                  </form>
               </div>
               <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Đóng</button>
                  <?php 
                     if(is_null(Yii::app()->user->id)){?>
                        <a class="btn btn-primary" href="/site/login">Đăng nhập</a>
                     <?php }else{?>
                        <button type="button" id="submit_review" class="btn btn-primary">Gửi đánh giá</button>
                     <?php }
                  ?>
                  
               </div>
             </div>
           </div>
         </div>
      </div>
   </div>
</div>
<script>
   $(document).ready(function(){
      var rate = 0;
      $(".rate").click(function(){
         var rev_content = $("#rev_content").val();
         rate = parseInt($(this).attr('rate'));
         var width = rate*20 + '%';
         $(this).attr('id','rated');
         $('#current_rating').css('width',width);
         
         
      });
      $("#submit_review").click(function(){
         rate = $("#rated").attr("rate");
         var rev_content = $("#rev_content").val();
         data = {'rate':rate,'rev_content':rev_content,'book_id':<?php echo $id?>};
         url = "/detail/review";
         succ = function(data){
            if(data.error == 0){
               $(".modal-body").html("Đánh giá của bạn đã được gửi thành công !");
               $("#submit_review").hide();
            }
         };
         handleAjax(url,"POST","json",data,succ);
      });
   });
</script>